package com.tzh.proxy;

import org.mybatis.generator.config.Configuration;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;

import java.lang.reflect.Method;

public class ConfigurationCGlibProxy implements MethodInterceptor {

    private Object targetObject;

    public <T> T CreateContextProxyObject(Configuration t) {
        targetObject = t;
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(t.getClass());
        enhancer.setCallback(this);
//        return (T)enhancer.create(new Class[]{Integer.class,String.class,String.class},new Object[]{0,null,null});
        return (T) enhancer.create();
    }


    @Override
    public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
        Object obj = null;
        obj = method.invoke(targetObject, args);
        System.out.println(method.getName());
        return obj;
    }


    public static void main(String[] args) {
//        ContextCGlibProxy proxy = new ContextCGlibProxy();
//        User user = proxy.CreateProxyObject(new User(1,"213","male"));
//        System.out.println(user.getName());
    }
}
